package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.gmc.global.g.rest.service.ICampaignSceneService;
import com.ibeeking.found.common.base.BaseDO;
import com.ibeeking.found.common.enums.marketing.CampaignSceneTypeEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.gmc.global.api.common.dto.CampaignSceneDTO;
import com.ibeeking.gmc.global.service.common.bo.CampaignSceneBO;
import com.ibeeking.gmc.global.service.common.dos.CampaignSceneDO;
import com.ibeeking.gmc.global.service.common.param.CampaignSceneParam;
import com.ibeeking.gmc.global.service.common.query.CampaignScenePageQuery;
import com.ibeeking.gmc.global.service.common.vo.CampaignSceneTypeVO;
import com.ibeeking.gmc.global.service.common.vo.CampaignSceneVO;
import com.ibeeking.gmc.global.service.mapper.CampaignSceneMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName CampaignSceneServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-07-08 9:14
 **/
@Service
public class CampaignSceneServiceImpl implements ICampaignSceneService {

    @Resource
    private CampaignSceneMapper campaignSceneMapper;

    @Override
    public Page<CampaignSceneVO> pageList(CampaignScenePageQuery query) {
        LambdaQueryWrapper<CampaignSceneDO> queryWrapper = new LambdaQueryWrapper<>(CampaignSceneDO.class)
                .orderByAsc(CampaignSceneDO::getSort)
                .orderByDesc(BaseDO::getCreateTime)
                .orderByDesc(BaseDO::getModifyTime)
                .eq(StringUtils.isNotBlank(query.getCode()), CampaignSceneDO::getCode, query.getCode())
                .like(StringUtils.isNotBlank(query.getName()), CampaignSceneDO::getName, query.getName());
        if (null != query.getSceneTypeCode()) {
            queryWrapper.eq(CampaignSceneDO::getSceneTypeCode, query.getSceneTypeCode().name());
        }
        Page<CampaignSceneDO> page = campaignSceneMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), queryWrapper);
        Page<CampaignSceneVO> result = BeanUtil.convertPage(page, CampaignSceneVO.class);
        if (!Collections3.isEmpty(result.getRecords())) {
            result.getRecords().stream().forEach(s -> s.setSceneTypeName(s.getSceneTypeCode().getLable()));
        }
        return result;
    }

    @Override
    public Boolean add(CampaignSceneParam param) {
        checkCode(param.getCode());
        CampaignSceneDO campaignSceneDO = BeanUtil.convertBean(param, CampaignSceneDO.class);
        campaignSceneDO.setSceneTypeCode(param.getSceneTypeCode().name());
        campaignSceneDO.setCreateBy(UserUtils.getUserId());
        campaignSceneDO.setCreateTime(LocalDateTime.now());
        campaignSceneDO.setModifyTime(LocalDateTime.now());
        return campaignSceneMapper.insert(campaignSceneDO) > 0;
    }

    @Override
    public Boolean modify(CampaignSceneParam param) {
        CampaignSceneDO campaignSceneDO = BeanUtil.convertBean(param, CampaignSceneDO.class);
        campaignSceneDO.setCode(null);
        campaignSceneDO.setSceneTypeCode(param.getSceneTypeCode().name());
        campaignSceneDO.setModifyBy(UserUtils.getUserId());
        campaignSceneDO.setModifyTime(LocalDateTime.now());
        return campaignSceneMapper.updateById(campaignSceneDO) > 0;
    }

    @Override
    public CampaignSceneVO view(Long id) {
        return BeanUtil.convertBean(getBean().queryById(id), CampaignSceneVO.class);
    }

    @Override
    public CampaignSceneBO queryById(Long id) {
        CampaignSceneDO campaignSceneDO = campaignSceneMapper.selectById(id);
        if (null == campaignSceneDO) {
            throw new BusinessException("活动场景不存在");
        }
        CampaignSceneBO campaignSceneBO = BeanUtil.convertBean(campaignSceneDO, CampaignSceneBO.class);
        CampaignSceneTypeEnum campaignSceneTypeEnum = CampaignSceneTypeEnum.valueOf(campaignSceneDO.getSceneTypeCode());
        campaignSceneBO.setSceneTypeCode(campaignSceneTypeEnum);
        campaignSceneBO.setSceneTypeName(campaignSceneTypeEnum.getLable());
        return campaignSceneBO;
    }

    @Override
    public Boolean del(Long id) {
        return campaignSceneMapper.deleteById(id) > 0;
    }

    @Override
    public List<CampaignSceneTypeVO> sceneTypes() {
        List<CampaignSceneTypeVO> result = new ArrayList<>();
        CampaignSceneTypeEnum[] values = CampaignSceneTypeEnum.values();
        Arrays.stream(values).forEach(s -> {
            CampaignSceneTypeVO campaignSceneTypeVO = new CampaignSceneTypeVO();
            campaignSceneTypeVO.setSceneTypeCode(s.name());
            campaignSceneTypeVO.setSceneTypeName(s.getLable());
            result.add(campaignSceneTypeVO);
        });
        return result;
    }

    @Override
    public List<CampaignSceneBO> selectBySceneByType(CampaignSceneTypeEnum campaignSceneType) {
        LambdaQueryWrapper<CampaignSceneDO> queryWrapper = new LambdaQueryWrapper<>(CampaignSceneDO.class)
                .orderByAsc(CampaignSceneDO::getSort)
                .orderByDesc(BaseDO::getCreateTime);
        if (null != campaignSceneType) {
            queryWrapper.eq(CampaignSceneDO::getSceneTypeCode, campaignSceneType.name());
        }
        return JsonUtils.jsonToList(campaignSceneMapper.selectList(queryWrapper), CampaignSceneBO.class);
    }

    @Override
    public List<CampaignSceneDTO> queryCampaignSceneByCode(List<String> codes) {
        List<CampaignSceneDO> campaignSceneDos = campaignSceneMapper.selectList(new LambdaQueryWrapper<>(CampaignSceneDO.class)
                .orderByAsc(CampaignSceneDO::getSort)
                .orderByDesc(BaseDO::getCreateTime)
                .in(CampaignSceneDO::getCode, codes)
        );
        return JsonUtils.jsonToList(campaignSceneDos, CampaignSceneDTO.class);
    }

    private void checkCode(String code) {
        CampaignSceneDO campaignSceneDO = campaignSceneMapper.selectOne(new LambdaQueryWrapper<>(CampaignSceneDO.class)
                .eq(CampaignSceneDO::getCode, code)
        );
        if (null != campaignSceneDO) {
            throw new BusinessException("活动场景编码重复");
        }
    }

    private static ICampaignSceneService getBean() {
        return SpringBeanUtil.getBean(ICampaignSceneService.class);
    }
}
